import React, { useEffect, useState } from 'react';
import { Redirect, Route } from 'react-router-dom';
import {
  getUserInfoDetail as getUserInfoDetailApi,
  getRoleList as getRoleListApi,
} from '../../API/index';
import { divide } from 'lodash-es';
import NotFound from '../pages/NotFound';
export default function AuthRoute(props) {
  const { children } = props;
  console.log(props);
  const allNavs = [
    {
      icon: 'monitor',
      title: '监测列表',
      link: '/monitor',
      subChildren: [
        '/form_page',
        '/securityInfo/',
        '/vul_detail/',
        '/outlink/Detail',
        '/change_detail/',
      ],
    },
    {
      icon: 'task',
      title: '任务管理',
      children: [
        {
          title: '任务队列',
          link: '/task_list',
        },
        {
          title: '任务组',
          link: '/task_group',
          subChildren: ['/task_info/', /form_page.*type=group/],
        },
      ],
    },
    {
      icon: 'alert',
      title: '告警中心',
      link: '/alert_Page',
    },
    {
      icon: 'down',
      title: '报告下载',
      link: '/down',
    },
    {
      icon: 'keyword',
      title: '策略管理',
      children: [
        {
          title: '敏感词',
          link: '/keyword',
        },
        {
          title: '漏洞模版',
          link: '/custom_module',
        },
        {
          title: '变更基准',
          link: '/change_set',
        },
      ],
    },
    {
      icon: 'user',
      title: '用户管理',
      children: [
        {
          title: '用户列表',
          link: '/user_manage',
        },
        {
          title: '账号设置',
          link: '/user_edit',
        },
        {
          title: 'API管理',
          link: '/api_page',
        },
        {
          title: '认证方式',
          link: '/auth_method',
        },
      ],
    },
    {
      icon: 'role',
      title: '角色管理',
      link: '/role_manage',
    },
    {
      icon: 'log',
      title: '日志中心',
      link: '/log_center',
    },
    {
      icon: 'backup',
      title: '备份恢复',
      link: '/backup_recovery',
    },
    {
      icon: 'bug',
      title: '漏洞库',
      link: '/bug_store',
    },
    {
      icon: 'network',
      title: '网络配置',
      children: [
        {
          title: '网口配置',
          link: '/network_config',
        },
        {
          title: '路由配置',
          link: '/router_config',
        },
        {
          title: 'DNS配置',
          link: '/dns_config',
        },
        {
          title: 'SNMP配置',
          link: '/snmp_config',
        },
        {
          title: 'SSH配置',
          link: '/ssh_config',
        },
        // {
        //   title: 'HTTPS配置',
        //   link: '/https_config',
        // },
      ],
    },
    {
      icon: 'update',
      title: '升级服务',
      link: '/system_update',
    },
    {
      icon: 'product',
      title: '系统信息',
      link: '/product_info',
    },
    {
      icon: 'systemStatus',
      title: '系统状态',
      children: [
        {
          title: '系统负载',
          link: '/system_load',
        },
        {
          title: '业务负载',
          link: '/business_load',
        },
        {
          title: '系统时间',
          link: '/system_time',
        },
        {
          title: '系统控制',
          link: '/system_control',
        },
        {
          title: '接口流量',
          link: '/interface_flow',
        },
      ],
    },
    {
      icon: 'remote',
      title: '问题排查',
      children: [
        // {
        //   title: '错误日志',
        //   link: '/error_log',
        // },
        {
          title: '远程协助',
          link: '/remote_help',
        },
      ],
    },
  ];
  const NavDataAdmin = [
    {
      icon: 'keyword',
      title: '策略管理',
      children: [
        {
          title: '敏感词',
          link: '/keyword',
        },
        {
          title: '漏洞模版',
          link: '/custom_module',
        },
        {
          title: '变更基准',
          link: '/change_set',
        },
      ],
    },
    {
      icon: 'user',
      title: '用户管理',
      children: [
        {
          title: '用户列表',
          link: '/user_manage',
        },
        {
          title: '账号设置',
          link: '/user_edit',
        },
        {
          title: 'API管理',
          link: '/api_page',
        },
        {
          title: '认证方式',
          link: '/auth_method',
        },
      ],
    },
    {
      icon: 'role',
      title: '角色管理',
      link: '/role_manage',
    },
    {
      icon: 'network',
      title: '网络配置',
      children: [
        {
          title: '网口配置',
          link: '/network_config',
        },
        {
          title: '路由配置',
          link: '/router_config',
        },
        {
          title: 'DNS配置',
          link: '/dns_config',
        },
        {
          title: 'SNMP配置',
          link: '/snmp_config',
        },
        {
          title: 'SSH配置',
          link: '/ssh_config',
        },
      ],
    },
    {
      icon: 'update',
      title: '升级服务',
      link: '/system_update',
    },
    {
      icon: 'product',
      title: '系统信息',
      link: '/product_info',
    },
    {
      icon: 'systemStatus',
      title: '系统状态',
      children: [
        {
          title: '系统负载',
          link: '/system_load',
        },
        {
          title: '业务负载',
          link: '/business_load',
        },
        {
          title: '系统时间',
          link: '/system_time',
        },
        {
          title: '系统控制',
          link: '/system_control',
        },
        {
          title: '接口流量',
          link: '/interface_flow',
        },
      ],
    },
    {
      icon: 'remote',
      title: '问题排查',
      children: [
        {
          title: '远程协助',
          link: '/remote_help',
        },
      ],
    },
  ];
  const NavDataActive = [
    {
      icon: 'monitor',
      title: '监测列表',
      link: '/monitor',
      subChildren: [
        '/form_page',
        '/securityInfo/',
        '/vul_detail/',
        '/outlink/Detail',
        '/change_detail/',
      ],
    },
    {
      icon: 'asset',
      title: '任务管理',
      children: [
        {
          title: '任务队列',
          link: '/task_list',
        },
        {
          title: '任务组',
          link: '/task_group',
          subChildren: ['/task_info/', /form_page.*type=group/],
        },
      ],
    },
    {
      icon: 'alert',
      title: '告警中心',
      link: '/alert_Page',
    },
    {
      icon: 'down',
      title: '报告下载',
      link: '/down',
    },
    {
      icon: 'user',
      title: '用户管理',
      children: [
        {
          title: '用户列表',
          link: '/user_manage',
        },
        {
          title: '账号设置',
          link: '/user_edit',
        },
        {
          title: 'API管理',
          link: '/api_page',
        },
        {
          title: '认证方式',
          link: '/auth_method',
        },
      ],
    },
    {
      icon: 'bug',
      title: '漏洞库',
      link: '/bug_store',
    },
    {
      icon: 'product',
      title: '系统信息',
      link: '/product_info',
    },
  ];
  const NavDataAudit = [
    {
      icon: 'user',
      title: '用户管理',
      children: [
        {
          title: '用户列表',
          link: '/user_manage',
        },
        {
          title: '账号设置',
          link: '/user_edit',
        },
        {
          title: 'API管理',
          link: '/api_page',
        },
        {
          title: '认证方式',
          link: '/auth_method',
        },
      ],
    },
    {
      icon: 'log',
      title: '日志中心',
      link: '/log_center',
    },
    {
      icon: 'bug',
      title: '漏洞库',
      link: '/bug_store',
    },
    {
      icon: 'product',
      title: '系统信息',
      link: '/product_info',
    },
  ];
  // const [currentUserId, setCurrentRoleId] = useState('');
  let currentUserId = '';

  const getUserInfoDetail = async () => {
    console.log(1);
    try {
      const { data } = await getUserInfoDetailApi();
      // setCurrentRoleId(data.user_group_id);
      currentUserId = data.user_group_id;
    } catch (err) {
      console.log(err);
    }
  };
  // getUserInfoDetail();
  const roleListParams = {
    page: 1,
    pageSize: 30,
    userGroupId: '',
  };
  const [navData, setNavData] = useState([]);
  // var navData = [];
  const setPermissionList = async () => {
    try {
      roleListParams.userGroupId = currentUserId;
      const { data } = await getRoleListApi(roleListParams);
      const resNavs = data.data[0].modules;
      const currentNavs = [];
      allNavs.forEach((item) => {
        const matchNav = resNavs.find((subItem) => subItem.module_name === item.title);
        if (matchNav) {
          currentNavs.push(item);
        }
      });

      setNavData(currentNavs);
      console.log('!!!!!!!!', currentNavs, navData);
      // navData = currentNavs;
    } catch (err) {
      console.log(err);
    }
  };
  // setPermissionList()
  let routerFlag = false;

  useEffect(async () => {
    console.log(localStorage.group);
    if (localStorage.group === '操作员') {
      setNavData(NavDataActive);
    } else if (localStorage.group === '审计员') {
      setNavData(NavDataAudit);
    } else if (localStorage.group === '管理员') {
      setNavData(NavDataAdmin);
    } else {
      await getUserInfoDetail();
      await setPermissionList();
    }
  }, []);
  console.log('!!!!!!!!', navData);

  // useEffect(() => {}, [navData]);
  traverseTreeNodes(navData);

  function traverseTreeNodes(treeArray) {
    // 遍历数组中的每一棵树
    console.log(treeArray);

    for (let tree of treeArray) {
      if (!routerFlag) {
        traverseTreeNode(tree);
      }
    }
    // 递归遍历单个树的节点
    function traverseTreeNode(node) {
      // 检查当前节点的link属性
      // if (routerFlag) {
      //   return;
      // }
      console.log(node);
      if (node.link && node.link === props.computedMatch.url) {
        console.log(node);
        routerFlag = true;
        return;
      }
      // 如果没有link属性或者link为空，并且存在子节点，则继续遍历子节点
      if (node.children && Array.isArray(node.children)) {
        for (let child of node.children) {
          traverseTreeNode(child);
        }
      }
    }
  }
  const whiteList = ['/login'];
  console.log(routerFlag, children);

  return <>{routerFlag ? children : <NotFound />}</>;
}
